/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2016-3-4         yellowlgx
 */

package com.skyworth.ui.blurbg;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.skyworth.commen.ui.R;
import com.skyworth.theme.widget.IThemeable;

/**
 * @author yellowlgx
 * @version TODO (write something)
 * @ClassName BlurBgLayout
 * @Description TODO (write something)
 * @date 2016-3-4
 */
public class BlurBgLayout extends FrameLayout implements IThemeable
{
    private Context mContext;
//    private BlurBgView bgView;
//    private BlurringView mBlurringView;
//    private ImageView mBlurView, mShadowLayout;
//    private LowMemoryBlurView mLowMemoryBlurView;

//    private int mDownsampleFactor;
//
//    private int curBlurRadiu = 1;
//
//    private int curOverLayoutAlpha = 125;
//
//    private float curAlpha = 1.0f, curShadowAlpha = 1.0f;
//
//    private String curOverColor = "ffffff";

    //    private FrameLayout.LayoutParams bgViewP;

    private Drawable bgDrawable;

//    private String pe;
//
//    private int version = Build.VERSION.SDK_INT;
//
//    private boolean mSaveBlurBitmap = false;

    @Override
    public void refreshOnThemeChanged()
    {
        setPageType(curType);
    }

    public enum PAGETYPE
    {
        FIRST_PAGE, SECONDE_PAGE, OTHER_PAGE
    }

    public BlurBgLayout(Context context)
    {
        this(context, null);
    }

    public BlurBgLayout(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public BlurBgLayout(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        mContext = context;
        initView();
    }

    private void initView()
    {
//        pe = SkySystemProperties.getProperty("ro.build.PE");
//        if (TextUtils.isEmpty(pe))
//        {
//            if (version >= 19)
//            {
//                bgView = new BlurBgView(mContext);
//                bgViewP = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
//                        LayoutParams.WRAP_CONTENT);
//                bgView.setLayoutParams(bgViewP);
//                this.addView(bgView);
////                mBlurringView = new BlurringView(mContext);
//                mDownsampleFactor = 10;
////                mBlurringView.setDownsampleFactor(mDownsampleFactor);
////                mBlurringView.setOnBlurFinishListener(this);
////                this.addView(mBlurringView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
////                        LayoutParams.MATCH_PARENT));
////                mBlurringView.setBlurredView(bgView);
////                bgView.setBlurringView(mBlurringView);
////                mShadowLayout = new ImageView(mContext);
////                this.addView(mShadowLayout, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
////                        LayoutParams.MATCH_PARENT));
////                mShadowLayout.setBackgroundResource(R.drawable.ui_sdk_app_bg_blur_shadow);
////                mShadowLayout.setVisibility(View.INVISIBLE);
//            } else
//            {
//                mLowMemoryBlurView = new LowMemoryBlurView(mContext);
//                this.addView(mLowMemoryBlurView, new FrameLayout.LayoutParams(
//                        LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//            }
//        } else
//        {
//            mBlurView = new ImageView(mContext);
//            this.addView(mBlurView, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
//                    LayoutParams.MATCH_PARENT));
//            mBlurView.setVisibility(View.INVISIBLE);
//        }
    }

    PAGETYPE curType = PAGETYPE.SECONDE_PAGE;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void setPageType(PAGETYPE type)
    {
        curType = type;
        bgDrawable = mContext.getResources().getDrawable(R.drawable.ui_sdk_main_page_bg_theme_2);

        switch (type)
        {
            case FIRST_PAGE:
                bgDrawable = getBgDrawable(R.drawable.ui_sdk_main_page_bg_theme_2);
                break;

            case SECONDE_PAGE:
                bgDrawable = getBgDrawable(R.drawable.ui_sdk_main_page_bg_theme_2);
                break;

            case OTHER_PAGE:
                bgDrawable = getBgDrawable(R.drawable.ui_sdk_other_page_bg_theme_2);
                break;

            default:
                break;
        }
        this.setBackgroundDrawable(bgDrawable);

//        if (TextUtils.isEmpty(pe))
//        {
//            bgDrawable = mContext.getResources().getDrawable(R.drawable.ui_sdk_main_page_bg);
//            if (version >= 19)
//            {
//                switch (type)
//                {
//                    case FIRST_PAGE:
//                        // curBlurRadiu = 6;
//                        // curShadowAlpha = 0.4f;
//                        // bgDrawable = mContext.getResources().getDrawable(
//                        // R.drawable.ui_sdk_main_page_bg);
//
//                        //
////                        bgDrawable = getResources().getDrawable(
////                                R.drawable.ui_sdk_main_page_bg_theme_2);
//                        bgDrawable = getBgDrawable(R.drawable.ui_sdk_main_page_bg_theme_2);
////                        mShadowLayout
////                                .setBackgroundResource(R.drawable.ui_sdk_app_bg_blur_shadow_theme_2);
////                        mShadowLayout.setBackground(bgDrawable);
//                        curBlurRadiu = 22;
//                        curShadowAlpha = 0.22f;
//
//                        setBgAlpha(1.0f);
//                        break;
//
//                    case SECONDE_PAGE:
//                        // curBlurRadiu = 6;
//                        // curShadowAlpha = 0.4f;
//                        // bgDrawable = mContext.getResources().getDrawable(
//                        // R.drawable.ui_sdk_main_page_bg);
//
//                        bgDrawable = getBgDrawable(R.drawable.ui_sdk_main_page_bg_theme_2);
//
////                        mShadowLayout.setBackground(bgDrawable);
////                        bgDrawable = getResources().getDrawable(
////                                R.drawable.ui_sdk_main_page_bg_theme_2);
////                        mShadowLayout
////                                .setBackgroundResource(R.drawable.ui_sdk_app_bg_blur_shadow_theme_2);
//                        curBlurRadiu = 22;
//                        curShadowAlpha = 0.22f;
//
//                        setBgAlpha(1.0f);
//                        break;
//
//                    case OTHER_PAGE:
//                        // curBlurRadiu = 6;
//                        // bgDrawable = mContext.getResources().getDrawable(
//                        // R.drawable.ui_sdk_other_page_bg);
//                        curBlurRadiu = 1;
//                        curShadowAlpha = 0.0f;
//                        bgDrawable = getBgDrawable(
//                                R.drawable.ui_sdk_other_page_bg_theme_2);
//                        setBgAlpha(0.95f);
//                        break;
//
//                    default:
//                        break;
//                }
//
//                curOverLayoutAlpha = 0;
//                setBlurRadius(curBlurRadiu);
//                setOverLayoutColor(curOverColor);
//                setBlurRes(bgDrawable);
////                mShadowLayout.setVisibility(View.VISIBLE);
//                setShadowLayoutAlpha(curShadowAlpha);
//            } else
//            {
//                switch (type)
//                {
//                    case FIRST_PAGE:
//                        bgDrawable = mContext.getResources().getDrawable(
//                                R.drawable.ui_sdk_main_page_bg_blur);
//                        setBgAlpha(1.0f);
//                        break;
//
//                    case SECONDE_PAGE:
//                        bgDrawable = mContext.getResources().getDrawable(
//                                R.drawable.ui_sdk_main_page_bg_blur);
//                        setBgAlpha(1.0f);
//                        break;
//
//                    case OTHER_PAGE:
//                        bgDrawable = mContext.getResources().getDrawable(
//                                R.drawable.ui_sdk_other_page_bg_blur);
//                        setBgAlpha(0.95f);
//                        break;
//
//                    default:
//                        break;
//                }
//                mLowMemoryBlurView.setBackgroundDrawable(bgDrawable);
//            }
//
//            this.post(new Runnable()
//            {
//                @Override
//                public void run()
//                {
//                    if (version >= 19)
//                    {
//                        int[] location = new int[2];
//                        BlurBgLayout.this.getLocationInWindow(location);
//                        bgViewP.leftMargin = -location[0];
//                        bgViewP.topMargin = -location[1];
//                        bgView.setLayoutParams(bgViewP);
//                        // bgView.refreshUI();
//                        mBlurringView.invalidate();
//                    }
//                }
//            });
//        } else
//        {
//            switch (type)
//            {
//                case FIRST_PAGE:
//                    mBlurView.setBackgroundResource(R.drawable.ui_sdk_app_first_pe_bg);
//                    setBgAlpha(1.0f);
//                    break;
//
//                case SECONDE_PAGE:
//                    mBlurView.setBackgroundResource(R.drawable.ui_sdk_app_second_pe_bg);
//                    setBgAlpha(1.0f);
//                    break;
//
//                case OTHER_PAGE:
//                    mBlurView.setBackgroundResource(R.drawable.ui_sdk_app_other_pe_bg);
//                    setBgAlpha(0.95f);
//                    break;
//
//                default:
//                    break;
//            }
//            mBlurView.setVisibility(View.VISIBLE);
//        }
    }

    private Drawable getBgDrawable(int resId)
    {
        Drawable drawable = getResources().getDrawable(resId);
//        drawable.setTileModeXY(Shader.TileMode.REPEAT , Shader.TileMode.REPEAT );
//        drawable.setDither(true);

        return drawable;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    public void setBgAlpha(float alpha)
    {
//        curAlpha = alpha;
//        this.setAlpha(curAlpha);
    }

    public void setShadowLayoutAlpha(float alpha)
    {
//        if (alpha >= 0 && alpha <= 1 && mShadowLayout != null
//                && mShadowLayout.getBackground() != null)
//        {
//            mShadowLayout.setAlpha(alpha);
//            // mShadowLayout.getBackground().setAlpha((int) (255 * alpha));
//        }
    }

    public void setBlurRadius(int radius)
    {
//        curBlurRadiu = radius;
//        if (version >= 19 && mBlurringView != null)
//        {
//            mBlurringView.setBlurRadius(curBlurRadiu);
//            mBlurringView.invalidate();
//        }
    }

    // public void setBlurRes(File file)
    // {
    // bgView.setImageViewRes(file);
    // }

    public void setCurTheme(boolean save)
    {
//        mSaveBlurBitmap = save;
//        try
//        {
//            String curTheme = ThemeUtil.getInstance().getCurTheme(getContext());
//            if (TextUtils.equals(curTheme, "theme_1"))
//            {
//                bgDrawable = getResources().getDrawable(R.drawable.ui_sdk_main_page_bg);
//                mShadowLayout.setBackgroundResource(R.drawable.ui_sdk_app_bg_blur_shadow);
//                curBlurRadiu = 6;
//                curShadowAlpha = 0.4f;
//            } else if (TextUtils.equals(curTheme, "theme_2"))
//            {
//                bgDrawable = getResources().getDrawable(R.drawable.ui_sdk_main_page_bg_theme_2);
//                mShadowLayout.setBackgroundResource(R.drawable.ui_sdk_app_bg_blur_shadow_theme_2);
//                curBlurRadiu = 22;
//                curShadowAlpha = 0.22f;
//            }
//            setBlurRadius(curBlurRadiu);
//            setBlurRes(bgDrawable);
//            setShadowLayoutAlpha(curShadowAlpha);
//        } catch (NameNotFoundException e)
//        {
//            e.printStackTrace();
//        }
    }

//    @Override
//    public void onBlurFinish(Bitmap blurBitmap)
//    {
//        if (mSaveBlurBitmap && blurBitmap != null)
//        {
//            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
//
//            Bitmap mBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(),
//                    Bitmap.Config.ARGB_8888);
//            Canvas shaderCanvas = new Canvas(mBitmap);
//
//            Matrix matrix = new Matrix();
//            matrix.postScale(mDownsampleFactor, mDownsampleFactor);
//            shaderCanvas.drawBitmap(blurBitmap, matrix, paint);
//
//            shaderCanvas.save();
//            Paint shaderPaint = new Paint();
//            Bitmap mShaderBitmap = BitmapFactory.decodeResource(getResources(),
//                    R.drawable.ui_sdk_page_bg_shadow_theme2);
//            BitmapShader mBitmapShader = new BitmapShader(mShaderBitmap, Shader.TileMode.REPEAT,
//                    Shader.TileMode.REPEAT);
//            shaderPaint.setShader(mBitmapShader);
//            shaderPaint.setAlpha((int) (curShadowAlpha * 255));
//            shaderCanvas.drawRect(0, 0, this.getWidth(), this.getHeight(), shaderPaint);
//            shaderCanvas.restore();
//
//            ThemeUtil.getInstance().savaBitmap(getContext(), mBitmap);
//        }
//    }

    public void setBlurRes(Drawable drawable)
    {
//        if (version >= 19)
//        {
//            if (bgView != null)
//                bgView.setBlurRes(drawable);
//            if (mBlurringView != null)
//                mBlurringView.invalidate();
//        }
    }

//    public BlurBgView getBlurBgView()
//    {
//        return null;// bgView;
//    }

    public void setOverLayoutCurAlpha(int alpha)
    {
//        curOverLayoutAlpha = alpha;
    }

    public int getOverLayoutCurAlpha()
    {
        return 0;//curOverLayoutAlpha;
    }

    public void setOverLayoutColor(String color)
    {
//        if (version >= 19)
//        {
//            curOverColor = color;
//            String r = color.substring(0, 2);
//            String g = color.substring(2, 4);
//            String b = color.substring(4, 6);
//            int overColor = Color.argb(curOverLayoutAlpha, Integer.valueOf(r, 16),
//                    Integer.valueOf(g, 16), Integer.valueOf(b, 16));
//            if (mBlurringView != null)
//            {
//                mBlurringView.setOverlayColor(overColor);
//                mBlurringView.invalidate();
//            }
//        }
    }

    public String getCurOverColor()
    {
        return null;// curOverColor;
    }

    public void destroy()
    {
//        if (mBlurringView != null)
//        {
//            mBlurringView.destroy();
//        }
    }
}